package com.horsen.service.user.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.user.controller.BaseController;
import com.horsen.service.user.model.BO.OrderOutputBO;
import com.horsen.service.user.model.DTO.OrderQueryDTO;
import com.horsen.service.user.model.Order;
import com.horsen.service.user.model.VO.OrderListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单 控制器
 *
 * @author Horsen
 */
@Api(tags = "订单管理")
@ApiSupport(order = 3)
@Slf4j
@RestController("AdminOrder")
@RequestMapping("/admin/user/orders")
public class OrderController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取所有订单")
    @Cacheable(key = "'adminQueryAllOrders'", value = "orderList")
    @GetMapping("/")
    public R queryAll() {
        List<Order> list = orderService.list();
        return list != null && list.size() != 0 ?
                R.ok("获取所有订单成功").setData("items", list) : R.error("获取所有订单失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("分页获取订单")
    @GetMapping("/{page}/{limit}")
    public R queryByPage(@ApiParam(value = "当前页码", required = true)
                         @PathVariable Long page,
                         @ApiParam(value = "每页记录数", required = true)
                         @PathVariable Long limit,
                         @ApiParam("订单查询对象") OrderQueryDTO orderQueryDTO) {
        IPage<OrderListVO> pageParam = orderService.pageSelect(page, limit, orderQueryDTO);
        List<OrderListVO> list = pageParam.getRecords();
        long total = pageParam.getTotal();
        return list != null ?
                R.ok("分页获取订单成功").setData("items", list).setData("total", total) : R.error("分页获取订单失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("导出订单到Excel文件")
    @PostMapping("/output")
    public R output(@ApiParam(value = "订单列表", required = true)
                    @RequestBody List<OrderOutputBO> list) {
        String filePath = orderService.output(list);
        return R.ok("导出讲师成功").setData("filePath", filePath);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("获取销售额")
    @GetMapping("/count")
    public R count() {
        BigDecimal count = orderService.countPaid();
        return R.ok("获取销售额成功").setData("count",count);
    }

}

